
package com.hoshiicloud.user.controller.company;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.company.BusinessAreasAttachmentType;
import com.hoshiicloud.user.param.company.BusinessAreasAttachmentTypeAddParam;
import com.hoshiicloud.user.param.company.BusinessAreasAttachmentTypeQueryParam;
import com.hoshiicloud.user.param.company.BusinessAreasAttachmentTypeUpdateParam;
import com.hoshiicloud.user.service.company.IBusinessAreasAttachmentTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * <p>
 * 经营领域附件类型business_areas_attachment_type控制器
 * </p>
 *
 * @author campchen
 * @since 2019-08-21
 */

@Slf4j
@RestController
@RequestMapping("/organization/business_areas_attachment_type")
@Api(value = "SAAS租户供应商经营领域附件类型", tags = {"SAAS租户供应商经营领域附件类型"})
public class BusinessAreasAttachmentTypeController {
    @Autowired
    private IBusinessAreasAttachmentTypeService businessAreasAttachmentTypeService;


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询经营领域附件类型business_areas_attachment_type
     */
    @ApiOperation(value = "分页查询经营领域附件类型business_areas_attachment_type")
    @PostMapping("/getPage")
    public Response page(@RequestBody BusinessAreasAttachmentTypeQueryParam param) {
        try {
            QueryWrapper<BusinessAreasAttachmentType> queryWrapper = new QueryWrapper<BusinessAreasAttachmentType>();
            queryWrapper.lambda()
                    .eq((param.getBusinessAreasId() != null && param.getBusinessAreasId() != 0), BusinessAreasAttachmentType::getBusinessAreasId, param.getBusinessAreasId())
                    .eq((param.getAttachmentId() != null && param.getAttachmentId() != 0), BusinessAreasAttachmentType::getAttachmentId, param.getAttachmentId())
                    .eq((param.getDeleted() != null && param.getDeleted() != 0), BusinessAreasAttachmentType::getDeleted, param.getDeleted());
            Page<BusinessAreasAttachmentType> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString() + ",Page{pageNumber=" + param.getPageNumber() + ",pageSize=" + param.getPageSize() + "}";
            return Response.data(businessAreasAttachmentTypeService.getPage(page, queryWrapper, key));
        } catch (Exception e) {
            log.error("分页查询经营领域附件类型business_areas_attachment_type异常", e);
            return Response.failed("分页查询经营领域附件类型business_areas_attachment_type异常");
        }
    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询经营领域附件类型business_areas_attachment_type
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询经营领域附件类型business_areas_attachment_type")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(@PathVariable("id") Long id) {
        return Response.data(businessAreasAttachmentTypeService.getById(id));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增经营领域附件类型business_areas_attachment_type")
    public Response save(@RequestBody @Valid BusinessAreasAttachmentTypeAddParam param) {
        BusinessAreasAttachmentType data;
        try {
            data = BeanUtils.covertType(param, BusinessAreasAttachmentType.class);
        } catch (Exception e) {
            log.error("新增机构经营领域附件类型business_areas_attachment_type", e);
            return Response.failed("新增经营领域附件类型business_areas_attachment_type Bean转换异常");
        }
        return Response.data(businessAreasAttachmentTypeService.save(data));
    }


    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改经营领域附件类型business_areas_attachment_type
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改经营领域附件类型business_areas_attachment_type")
    public Response update(@RequestBody @Valid BusinessAreasAttachmentTypeUpdateParam param) {
        BusinessAreasAttachmentType data;
        try {
            data = BeanUtils.covertType(param, BusinessAreasAttachmentType.class);
        } catch (Exception e) {
            log.error("修改经营领域附件类型business_areas_attachment_type Bean转换异常", e);
            return Response.failed("修改经营领域附件类型business_areas_attachment_type Bean转换异常");
        }
        return Response.data(businessAreasAttachmentTypeService.updateById(data));
    }

}

